package com.huixh.train.member.controller;

import com.huixh.train.common.resp.MemberLoginResp;
import com.huixh.train.common.resp.Result;
import com.huixh.train.member.req.MemberLoginReq;
import com.huixh.train.member.req.MemberRegisterReq;
import com.huixh.train.member.service.MemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author huixh
 */

@Validated
@RestController
@Tag(name = "MemberController", description = "会员管理")
@RequestMapping("/member")
public class MemberController {
    @Resource
    private MemberService memberService;

    @GetMapping("/count")
    @Operation(summary = "会员总数", method = "GET")
    public Result<Long> memberCount() {
        return Result.success(1L);
    }

    @PostMapping("/register")
    @Operation(summary = "会员注册", method = "POST")
    public Result<Long> register(@RequestBody @Valid MemberRegisterReq memberRegisterReq) {
        Long aLong = memberService.register(memberRegisterReq);
        return Result.success(aLong);
    }

    @GetMapping("/sendCode")
    @Operation(summary = "验证码发送", method = "GET")
    public Result<?> sendCode(@NotBlank(message = "参数不能为空")
                              @Pattern(regexp = "^1[3456789]\\d{9}$", message = "手机号码格式错误")
                              String mobile) {
        memberService.sendCode(mobile);
        return Result.success("验证码发送成功");
    }

    @PostMapping("/login")
    @Operation(summary = "会员登录", method = "POST")
    public Result<MemberLoginResp> login(@RequestBody @Valid MemberLoginReq memberLoginReq) {
        MemberLoginResp resp = memberService.login(memberLoginReq);
        return Result.success(resp);
    }

}